package cn.wolfcode.luowowo.website.web.controller;


import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.Answer;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.AnswerQuery;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IAnswerService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.community.query.QuestionQuery;
import cn.wolfcode.luowowo.community.service.IQuestionService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 问答控制器
 */
@Controller
@RequestMapping("wenda")
public class QuestionController {

    @Reference
    private IQuestionService questionService;

    @Reference
    private IQuestionRedisService questionRedisService;

    @Reference
    private IAnswerService answerService;

    //问答首页
    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") QuestionQuery qo, @UserParam UserInfo userInfo){
        //pageInfo
        model.addAttribute("pageInfo",questionService.query(qo));
        model.addAttribute("userInfo",userInfo);
        return "community/wenda";
    }

    //我要提问
    @RequireLogin
    @RequestMapping("input")
    public String input(Model model,@UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfo);
        return "community/public";
    }

    //发布问题
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Question question, @UserParam UserInfo user){
        if(user == null){
            return new AjaxResult(false,"请先登录!");
        }
        question.setAuthor(user);
        Long id = questionService.save(question);
        return AjaxResult.SUCCESS.addData(id);
    }

    //问题的明细
    @RequestMapping("detail")
    public String detail(Model model,Long id,@ModelAttribute("qo") AnswerQuery qo,@UserParam UserInfo userInfo){
        //
        Question question = questionService.get(id);
        question.setQuestionContent(questionService.getContentByQuestionId(id));
        model.addAttribute("qt",question);
        qo.setQuestionId(id);
        Page page = answerService.query(qo);
        model.addAttribute("page",page);
        model.addAttribute("userInfo",userInfo);
        return "community/wendaDetail";
    }

    //顶(点赞)
    @RequestMapping("/wendaThumbup")
    @ResponseBody
    public Object wendaThumbup(@UserParam UserInfo userInfo,Long qid){
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false, "请您先登录");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        boolean result = questionRedisService.addThumbsupnum(qid, userInfo.getId());
        if (result){
            questionRedisService.addQuestionScore(qid,1);
        }
        return new AjaxResult(result,"").addData(questionRedisService.getQuestionStatisVo(qid));
    }

    //添加回答
    @RequestMapping("/addAnswer")
    @ResponseBody
    public Object addAnswer(@UserParam UserInfo userInfo, Answer answer,Long questionId){
        if (userInfo == null){
            throw new LogicException("请先登录");
        }
        answer.setQuestionId(questionId);
        answer.setUserId(userInfo.getId());
        answer.setHeadUrl(userInfo.getHeadImgUrl());
        answer.setLevel(userInfo.getLevel());
        answer.setCity(userInfo.getCity());
        answer.setUsername(userInfo.getNickname());
        answerService.save(answer);
        return AjaxResult.SUCCESS.addData(answer.getQuestionId());
    }




}




